//
//  GradeViewController.m
//  FloatClouds
//
//  Created by 黄威 on 16/9/12.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "GradeViewController.h"
#import "BaseButton.h"
#import "BaseLabel.h"
#import "HWLNetworkTool.h"
#import "CenterViewController.h"
@interface GradeViewController()

@property (nonatomic, strong) UISlider *sliderOfTaste;
@property (nonatomic, strong) UISlider *sliderOfView;
@property (nonatomic, strong) UISlider *sliderOfPrice;

@property (nonatomic, strong) UILabel *labelOfTaste;
@property (nonatomic, strong) UILabel *labelOfView;
@property (nonatomic, strong) UILabel *labelOfPrice;

@property (nonatomic, strong) UILabel *labelOfTasteInput;
@property (nonatomic, strong) UILabel *labelOfViewInput;
@property (nonatomic, strong) UILabel *labelOfInputPrice;

@property (nonatomic, strong) BaseButton *button;


@end



@implementation GradeViewController


- (void)viewDidLoad{

    self.labelOfTaste = [[BaseLabel alloc]init];
   // self.labelOfTaste.textColor = [UIColor grayColor];
    self.labelOfTaste.text = @"   口味:";
    self.labelOfTaste.font = [UIFont fontWithName:@"Helvetica" size:17];
    [self.view addSubview:self.labelOfTaste];
    self.labelOfTaste.frame = CGRectMake(0, 10 +64, self.view.bounds.size.width *0.2, self.view.bounds.size.width *0.1);

     self.labelOfView = [[BaseLabel alloc]init];
    // self.labelOfView.textColor = [UIColor grayColor];
     self.labelOfView.text = @"   外观:";
     self.labelOfView.font = [UIFont fontWithName:@"Helvetica" size:17];
    [self.view addSubview: self.labelOfView];
     self.labelOfView.frame = CGRectMake(0, self.view.bounds.size.width *0.1 + 74, self.view.bounds.size.width *0.2, self.view.bounds.size.width *0.1);
    
    self.labelOfTaste = [[BaseLabel alloc]init];
  //  self.labelOfTaste.textColor = [UIColor grayColor];
    self.labelOfTaste.text = @"性价比:";
    self.labelOfTaste.font = [UIFont fontWithName:@"Helvetica" size:17];
    [self.view addSubview:self.labelOfTaste];
    self.labelOfTaste.frame = CGRectMake(0, self.view.bounds.size.width *0.2 + 74, self.view.bounds.size.width *0.2, self.view.bounds.size.width *0.1);




    self.sliderOfTaste = [[UISlider alloc]init];
    self.sliderOfTaste.maximumValue = 10;
    self.sliderOfTaste.minimumValue = 0;
    [self.view addSubview:self.sliderOfTaste];
    self.sliderOfTaste.frame =  CGRectMake(self.view.bounds.size.width *0.2, 10 +64, self.view.bounds.size.width *0.6, self.view.bounds.size.width *0.1);
    
    self.sliderOfView = [[UISlider alloc]init];
    self.sliderOfView.maximumValue = 10;
    self.sliderOfView.minimumValue = 0;
    [self.view addSubview:self.sliderOfView];
    self.sliderOfView.frame =  CGRectMake(self.view.bounds.size.width *0.2, self.view.bounds.size.width *0.1 + 74, self.view.bounds.size.width *0.6, self.view.bounds.size.width *0.1);
    
    self.sliderOfPrice = [[UISlider alloc]init];
    self.sliderOfPrice.maximumValue = 10;
    self.sliderOfPrice.minimumValue = 0;
    [self.view addSubview:self.sliderOfPrice];
    self.sliderOfPrice.frame =  CGRectMake(self.view.bounds.size.width *0.2, self.view.bounds.size.width *0.2 + 74, self.view.bounds.size.width *0.6, self.view.bounds.size.width *0.1);
    
    
    
    self.labelOfTasteInput = [[BaseLabel alloc]init];
    self.labelOfTasteInput.font = [UIFont fontWithName:@"Helvetica" size:17];
    [self.view addSubview:self.labelOfTasteInput];
    self.labelOfTasteInput.frame = CGRectMake(self.view.bounds.size.width *0.9, 10 +64, self.view.bounds.size.width *0.2, self.view.bounds.size.width *0.1);
    self.labelOfTasteInput.text = @"0";
    
    self.labelOfViewInput = [[BaseLabel alloc]init];
    self.labelOfViewInput.font = [UIFont fontWithName:@"Helvetica" size:17];
    [self.view addSubview: self.labelOfViewInput];
    self.labelOfViewInput.frame = CGRectMake(self.view.bounds.size.width *0.9, self.view.bounds.size.width *0.1 + 74, self.view.bounds.size.width *0.2, self.view.bounds.size.width *0.1);
    self.labelOfViewInput.text = @"0";
   
    self.labelOfInputPrice = [[BaseLabel alloc]init];
    self.labelOfInputPrice.font = [UIFont fontWithName:@"Helvetica" size:17];
    [self.view addSubview:self.labelOfInputPrice];
    self.labelOfInputPrice.frame = CGRectMake(self.view.bounds.size.width *0.9 , self.view.bounds.size.width *0.2 + 74, self.view.bounds.size.width *0.2, self.view.bounds.size.width *0.1);
    self.labelOfInputPrice.text = @"0";
    
    
    self.button = [[BaseButton alloc]initWithFrame:CGRectMake(self.view.bounds.size.width *0.2, self.view.bounds.size.width *0.3 + 104, self.view.bounds.size.width *0.6 ,self.view.bounds.size.width * 0.1)];
    [self.button.layer setMasksToBounds:YES];
    [self.button.layer setCornerRadius:5.0];
     self.button.backgroundColor = [UIColor colorWithRed:18.0 / 255 green:86.0 / 255 blue:138.0 / 255 alpha:1];
    [self.button setTitleColor:[ UIColor whiteColor] forState:UIControlStateNormal];
    [self.button setTitle:@"发表" forState:UIControlStateNormal];
    [self.view addSubview:self.button];
    
    [self.button addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.sliderOfTaste addTarget:self action:@selector(sliderchanged1:) forControlEvents:UIControlEventValueChanged];
    [self.sliderOfView addTarget:self action:@selector(sliderchanged2:) forControlEvents:UIControlEventValueChanged];
    [self.sliderOfPrice addTarget:self action:@selector(sliderchanged3:) forControlEvents:UIControlEventValueChanged];
   

}

- (void)sliderchanged1:(UISlider *)slider{

    int value = (int)(slider.value + 0.5f);
    self.labelOfTasteInput.text = [NSString stringWithFormat:@"%d",value];
  
}

- (void)sliderchanged2:(UISlider *)slider{
    
    int value = (int)(slider.value + 0.5f);
    self.labelOfViewInput.text = [NSString stringWithFormat:@"%d",value];
    
}
- (void)sliderchanged3:(UISlider *)slider{
    
    int value = (int)(slider.value + 0.5f);
    self.labelOfInputPrice.text = [NSString stringWithFormat:@"%d",value];
    
}
    
- (void)submit{


/*
 appearance_grade=10&cigarette_id=553&cookie=430ctu9QkloFC3KxrMhFamicOS887%2BCPT%2B1vURUi5fX8n3hHASW1PFbhpg&cost_performance=10&taste_grade=10&uid=887426
 */
    NSDictionary *dicOfHeader = @{@"Content-Type":@"application/x-www-form-urlencoded"};

        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *pathCookie = [[path lastObject]stringByAppendingPathComponent:@"cookie.TXT"];
    
       NSString *pathuuid = [[path lastObject]stringByAppendingPathComponent:@"uid.TXT"];
    
    NSString *cookie = [NSString stringWithContentsOfFile:pathCookie encoding:NSUTF8StringEncoding error:nil];
    
    NSString *uid = [NSString stringWithContentsOfFile:pathuuid encoding:NSUTF8StringEncoding error:nil];

    
    if (cookie == nil | uid == nil) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"尚未登录,不能评论哦" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"不了,谢谢!" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"我现在登录" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            CenterViewController *center = [[CenterViewController alloc]init];
            
            [self.navigationController pushViewController:center animated:YES];
            
        }]];
        
        [self presentViewController:alert animated:YES completion:nil];

        
        
    }else{
    
    NSString *body = [[[[[[[[[[[@"cigarette_id="stringByAppendingString:self.productID]stringByAppendingString:@"&appearance_grade="]stringByAppendingString:self.labelOfViewInput.text]stringByAppendingString:@"&taste_grade="]stringByAppendingString:self.labelOfTasteInput.text]stringByAppendingString:@"&cost_performance="]stringByAppendingString:self.labelOfInputPrice.text]  stringByAppendingString:@"&cookie="]stringByAppendingString:cookie]stringByAppendingString:@"&uid="]stringByAppendingString:uid];
    
    
   
    
    
    
    
    
    [HWLNetworkTool POST:@"http://app.yanyue.cn/product/score" andBody:body andBodyStyle:HWLBodyString andHeader:dicOfHeader andResponse:HWLJSON andSuccessBlock:^(id result) {
        
        
     
        
        NSInteger status = [result[@"status"] integerValue];
        
        if (status == 0) {
            
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"评论失败" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
          
            [self presentViewController:alert animated:YES completion:nil];
   
        }
        if (status == 2) {
            
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录超时需要重新登录哦" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"不了,谢谢!" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"我现在登录" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                CenterViewController *center = [[CenterViewController alloc]init];
                
                [self.navigationController pushViewController:center animated:YES];
                
            }]];
            
            [self presentViewController:alert animated:YES completion:nil];
            
            
            
        }

        
        if (status == 1) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"评论成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController popViewControllerAnimated:YES];
            }]];
            
            [self presentViewController:alert animated:YES completion:nil];
            
        }
        
        
        
       
        
    } andFailureBlock:^(NSError *error) {
        
    }];


    }

}


@end
